package controller;

import java.io.IOException;

import model.searchers.ISearcher;
import utilityClasses.FileOutput;
import view.IView;

public class Controller {

	private ISearcher model;
	private IView view;
	/**
	 * Creates the controller used in this instance of the program.
	 * @param m Reference to the searcher used in this instance of the program.
	 * @param v Reference to the view used in this instance of the program.
	 */
	public Controller(ISearcher m) {
		model = m;
	}

	/**
	 * Receives formatted search result from the ISearcher view (model) and passes this to the view to show.
	 * @param searchWord The search string that we want to look up. 
	 */
	public void doSearch(String searchWord) {
		String[] result = model.getFormattedResult(searchWord);
		view.showResult(result);
	}

	/**
	 * Sets a reference to the view used in this instance of the program.
	 * @param v
	 */
	public void setView(IView v) {
		view = v;
	}

	/**
	 * Calls the getSample method of the ISearcher view (model).
	 * @param sampleSize The size of the sample of search words we want to search for in the benchmark.
	 * @return Returns an array of Strings of length sampleSize with random search words.
	 */
	public String[] getSample(int sampleSize) {
		return model.createSample(sampleSize);
	}
	public long reLoadData(String fileName) {
		try {
			return model.loadData(fileName);
		} catch (IOException e) {
			System.out.println("Could not reload data!");
			e.printStackTrace();
			return -1;
		}
	}
	
	@Override
	public String toString() {
		return "View Type: " + view.toString() +"\r\n"+ model.toString() ;
	}
	
	public boolean isBooleanSearch() {
		return model.isBooleanSearch();
	}
	
	public String getFileName() {
		return model.getFileName();
	}

}
